package com.platform.server.service.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.server.dao.TopicDao;
import com.platform.server.dao.UserDao;
import com.platform.server.dao.VideoDao;
import com.platform.server.entity.TopicComment;
import com.platform.server.entity.User;
import com.platform.server.entity.VideoComment;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.service.TopicService;
import com.platform.server.util.StringTools;

@Service
@Transactional
public class TopicServiceImpl implements TopicService {

	@Autowired
	private TopicDao topicDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private VideoDao videoDao;
	/**
	 * 参与话题
	 */
	@Override
	public ReturnData joinTopic(String topicid, String userid) {
		if(StringTools.isEmpty(topicid) || StringTools.isEmpty(userid)){
			return new ReturnData(12,"参数有误");
		}
		int flag = topicDao.joinTopic(topicid, userid);
		if(flag == 1){
			return new ReturnData(200,"参与成功");
		}
		return new ReturnData(100,"参与失败");
	}

	/**
	 * 话题点赞
	 */
	@Override
	public ReturnData topicLike(String topic_dynamic_id, String userid) {
		if(StringTools.isEmpty(topic_dynamic_id) || StringTools.isEmpty(userid)){
			return new ReturnData(12,"参数有误");
		}
		//查看是否已经点赞
		int count = topicDao.selectIsLike(topic_dynamic_id, userid);
		if(count > 0){
			return new ReturnData(18,"已点赞");
		}
		int flag = topicDao.topicLike(topic_dynamic_id, userid);
		if(flag == 1){
			return new ReturnData(200,"点赞成功");
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 热门话题列表
	 */
	@Override
	public ReturnData hotTopicList() {
		List<Map<String, Object>> lists = topicDao.hotTopicList();
		if(lists.size() > 0){
			return new ReturnData(lists,200,"成功");
		}
		return new ReturnData(100,"暂无数据");
	}

	/**
	 * 话题详情
	 */
	@Override
	public ReturnData selectTopicInfo(Integer topicid,String userid) {
		if(topicid == null){
			return new ReturnData(12,"参数有误");
		}
		List<Map<String, Object>> lists = topicDao.selectTopicInfo(topicid);
		if(lists.size() == 0){
			return new ReturnData(100,"暂无数据");
		}
		//查看用户是否点赞了该话题动态
		int isLike = topicDao.selectUserLike(topicid.toString(), userid);
		//查看用户是否参与了话题
		int isJoin = topicDao.selectUserJoin(topicid.toString(),userid);
		Map<String,Object> hashMap = new HashMap<>();
		hashMap.put("lists", lists);
		hashMap.put("isJoin", isJoin);
		hashMap.put("isLike", isLike);
		return new ReturnData(hashMap,200,"成功");
	}

	/**
	 * 发表话题评论
	 */
	@Override
	public ReturnData insertTopicComment(String topicid, String userid, String content) {
		if(StringTools.isEmpty(topicid) || StringTools.isEmpty(userid) || StringTools.isEmpty(content)){
			return new ReturnData(12,"参数有误");
		}
		int flag = topicDao.insertTopicComment(topicid, userid, content);
		if(flag == 1){
			return new ReturnData(200,"成功");
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 发表话题动态评论
	 */
	@Override
	public ReturnData insertCommentPrimary(TopicComment topicComment) {
		if(StringTools.isEmpty(topicComment.getTopic_dynamic_id()) || StringTools.isEmpty(topicComment.getSender_id())
				|| StringTools.isEmpty(topicComment.getContent())){
			return new ReturnData(12,"参数有误");
		}
		topicComment.setCreate_time(new Date());
		TopicComment topicComment2 = null;
		if(!StringTools.isEmpty(topicComment.getParent_id())){
			//先找到父评论
			topicComment2 = topicDao.selectParentComment(Integer.parseInt(topicComment.getParent_id())); 
            if(topicComment2 == null){
                return new ReturnData(10,"未找到你要评论的评论");
            }else {
           	 //设置该评论路径
                if(StringTools.isEmpty(topicComment2.getPath())){  //设置评论路径。如果父评论是一级评论
               	 	topicComment.setPath(","+topicComment2.getId()+",");
                }else { //如果父评论是多级评论,就在父评论的基础上再加一级
                	topicComment.setPath(topicComment2.getPath()+topicComment2.getId()+",");
                }
            }
        }
		int flag = topicDao.insertCommentPrimary(topicComment);
		if(flag == 1){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("recordid", topicComment.getId());
			//评论或回复成功之后添加消息表记录
			int i = topicDao.insertCommentRecord(map);
			if(i == 1){
				return new ReturnData(200,"成功");
			}
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 查看评论消息列表
	 */
	@Override
	public ReturnData selectCommentList(String userid) {
		if(StringTools.isEmpty(userid)){
			return new ReturnData(12,"参数有误");
		}
		//查看话题评论或回复通知
		List<Map<String,Object>> list1 = topicDao.topicCommentAdvice(userid);
		for(int i = 0;i < list1.size();i++){
			list1.get(i).put("m_type", "4");
		}
		//查看视频评论或回复通知
		List<Map<String, Object>> list2 = topicDao.videoCommentAdvice(userid);
		for(int i = 0;i < list2.size();i++){
			list2.get(i).put("m_type", "5");
		}
		list2.addAll(list1);
		Collections.sort(list2, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            	return ((Date) o2.get("create_time")).compareTo((Date) o1.get("create_time"));
            }
	    });
		return new ReturnData(list2,200,"成功");
	}

	/**
	 * 获取话题的推荐用户
	 */
	@Override
	public ReturnData selectTopicRecommendUser() {
		List<Map<String, Object>> lists = topicDao.selectTopicRecommendUser();
		for(int i = 0;i < lists.size();i++){
			if(lists.get(i).get("star_level") == null){
				lists.get(i).put("star_level", 4);
			}
		}
		return new ReturnData(lists,200,"成功");
	}

	/**
	 * 评论详情
	 */
	@Override
	public ReturnData selectCommentInfo(String topicid,String userid) {
		if(StringTools.isEmpty(topicid)){
			return new ReturnData(12,"参数有误");
		}
		List<TopicComment> lists = topicDao.selectCommentInfo(topicid);
		for(int i = 0;i < lists.size();i++){
			//查看话题评论中的父评论
			TopicComment videoComment = topicDao.queryParentList(Integer.parseInt(lists.get(i).getParent_id()));
			lists.get(i).setTopicComment(videoComment);
			//查看评论人信息
			User suser = topicDao.selectSenderInfo(Integer.parseInt(lists.get(i).getSender_id()));
			lists.get(i).setSenderUser(suser);
			//查看回复人信息
			String rid = lists.get(i).getReceiver_id();
			if(!StringTools.isEmpty(rid)){
				User ruser = topicDao.selectSenderInfo(Integer.parseInt(rid));
				lists.get(i).setReceiverUser(ruser);
			}
		}
		//查看用户是否点赞了该话题动态
		int isLike = topicDao.selectUserLike(topicid, userid);
		//查看话题动态详情
		Map<String, Object> map = topicDao.selectTopicdyInfo(Integer.parseInt(topicid));
		if(lists.size() > 0){
			map.put("lists", lists);
		}else{
			map.put("lists", "");
		}
		map.put("isLike", isLike);
		return new ReturnData(map,200,"成功");
	}

	/**
	 * 退出话题
	 */
	@Override
	public ReturnData exitTopic(String topicid, String userid) {
		if(StringTools.isEmpty(topicid) || StringTools.isEmpty(userid)){
			return new ReturnData(12,"参数有误");
		}
		int flag = topicDao.exitTopic(topicid, userid);
		if(flag > 0){
			return new ReturnData(200,"成功");
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 个人资料详情
	 */
	@Override
	public ReturnData selectDatumDetails(Integer detailsUserid,String userid) {
		Map<String, Object> hashMap = new HashMap<>();
		//个人资料详情
		Map<String, Object> map = topicDao.selectDatumDetails(detailsUserid);
		if(map.get("star_level") == null){
			map.put("star_level", 4);
		}
		//查看是否是我关注的人
		int count = userDao.selectMyFansIsattention(userid,detailsUserid.toString());
		map.put("isAttention", count);
		List<Map<String, Object>> lists = videoDao.selectVideoList(detailsUserid.toString());
		hashMap.put("info", map);
		hashMap.put("videoList", lists);
		//查看两个用户之间有没有预约过服务
		int appointment = topicDao.isAppointment(detailsUserid.toString(), userid);
		if(appointment==0){
			appointment = topicDao.isAppointment2(detailsUserid.toString(), userid);
		}
		if(!StringTools.isEmpty(userid)){
			
			int selectUserRelation = userDao.selectUserRelation(Integer.parseInt(userid),detailsUserid);
			if(selectUserRelation>0){
				hashMap.put("relation", 1);
			}else{
				hashMap.put("relation", 0);
			}
		}
		hashMap.put("appointment", appointment);
		return new ReturnData(hashMap,200,"成功");
	}

	/**
	 * 删除话题评论
	 */
	@Override
	public ReturnData deleteTopicComment(Integer id,Integer type) {
		if(id == null || type == null){
			return new ReturnData(12,"参数有误");
		}
		if(type == 1){
			int flag = topicDao.deleteTopicComment(id);
			if(flag > 0){
				return new ReturnData(200,"删除成功");
			}
		}else if(type == 2){//删除话题动态
			int flag = topicDao.deleteTopicDynamic(id);
			if(flag > 0){
				return new ReturnData(200,"删除成功");
			}
		}
		return new ReturnData(100,"删除失败");
	}

}
